package com.longcai.util;


import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.JdException;
import jd.union.open.goods.jingfen.query.request.JFGoodsReq;
import jd.union.open.goods.jingfen.query.request.UnionOpenGoodsJingfenQueryRequest;
import jd.union.open.goods.jingfen.query.response.JFGoodsResp;
import jd.union.open.goods.jingfen.query.response.UnionOpenGoodsJingfenQueryResponse;
import jd.union.open.order.query.request.OrderReq;
import jd.union.open.order.query.request.UnionOpenOrderQueryRequest;
import jd.union.open.order.query.response.OrderResp;
import jd.union.open.order.query.response.UnionOpenOrderQueryResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 京东精选公共类
 * </p>
 *
 * @author LWK
 * @since 2019-03-20
 */
public class ChoiceJDUtil {
    private static final String SERVER_URL_JD = "https://router.jd.com/api";            //服务地址
    private static final String APPKEY_JD = "31268b240a174e21b684e1bab5162e6c";            //联盟分配给应用的app_key
    private static final String APPSECRET_JD = "b62844ab58c546f0aa0532ba8bd1f1dc";        //应用app密钥
    private static final String ACCESS_TOKEN_JD = "";                                    //根据API属性标签，如果需要授权，则此参数必传;如果不需要授权，则此参数不需要传

    /**
     * 京东联盟精选优质商品，每日更新，可通过频道ID查询各个频道下的精选商品
     *
     * @param    eliteId        频道id
     * @param    sortName    排序字段
     * @param    sort        升降序
     * @param    pageSize    每页数量
     * @param    pageIndex    页码
     */
    public static Map<String, Object> getJdData(Integer eliteId, String sortName, String sort, Integer pageSize, Integer pageIndex) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            JdClient client = new DefaultJdClient(SERVER_URL_JD, ACCESS_TOKEN_JD, APPKEY_JD, APPSECRET_JD);
            //定义入参
            UnionOpenGoodsJingfenQueryRequest request = new UnionOpenGoodsJingfenQueryRequest();
            JFGoodsReq goodsReq = new JFGoodsReq();
            if (eliteId != null && eliteId != 0) {
                goodsReq.setEliteId(eliteId);
            }
            if (sortName != null && sortName != "") {
                goodsReq.setSortName(sortName);
            }
            if (sort != null && sort != "") {
                goodsReq.setSort(sort);
            }
            if (pageSize != null && pageSize != 0) {
                goodsReq.setPageSize(pageSize);
            }
            if (pageIndex != null && pageIndex != 0) {
                goodsReq.setPageIndex(pageIndex);
            }
            request.setGoodsReq(goodsReq);
            //执行操作
            UnionOpenGoodsJingfenQueryResponse response = client.execute(request);
            //定义结果类型
            resultMap.put("code", response.getCode());
            resultMap.put("msg", response.getMessage());
            JFGoodsResp[] data = response.getData();
            resultMap.put("data", data);
        } catch (JdException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultMap;
    }

    //京东
//   public static void main(String[] args) {
//	   try {
//关键词商品查询
//		   String SERVER_URL = "https://router.jd.com/api";
//		   String appKey = "31268b240a174e21b684e1bab5162e6c";
//		   String appSecret ="b62844ab58c546f0aa0532ba8bd1f1dc";
//		   String accessToken = "";
//
//		   JdClient client=new DefaultJdClient(SERVER_URL,accessToken,appKey,appSecret); 
//
//		   UnionOpenGoodsQueryRequest request = new UnionOpenGoodsQueryRequest();
//		   GoodsReq goodsReq = new GoodsReq();
//		   goodsReq.setSkuIds(new Long[]{6877440L,844529L});
//
//		   request.setGoodsReqDTO(goodsReq);
//		   UnionOpenGoodsQueryResponse response = client.execute(request);
//		   String SERVER_URL = "https://router.jd.com/api";
//		   String appKey = "31268b240a174e21b684e1bab5162e6c";
//		   String appSecret ="b62844ab58c546f0aa0532ba8bd1f1dc";
//		   String accessToken = "";
//	
//		   JdClient client=new DefaultJdClient(SERVER_URL,accessToken,appKey,appSecret); 

//		   UnionOpenCouponQueryRequest request = new UnionOpenCouponQueryRequest();
//		   request.setCouponUrls(new String[]{"http://coupon.jd.com/ilink/couponActiveFront/front_index.action?key=61c19ff15ceb450c8d801c50f729069d&roleId=7290729&to=jyhbj.jd.hk"});
//		   UnionOpenCouponQueryResponse  response = client.execute(request);

    //		   UnionOpenGoodsJingfenQueryRequest request = new UnionOpenGoodsJingfenQueryRequest();
//		   JFGoodsReq goodsReq = new JFGoodsReq();
//		   goodsReq.setEliteId(11);
//		   request.setGoodsReq(goodsReq);
//		   UnionOpenGoodsJingfenQueryResponse  response = client.execute(request);
//		   
//		   System.out.println("返回状态："+response.getCode());
//		   System.out.println("返回结果："+response.getMessage());
//		   JFGoodsResp[] data = response.getData();
//		   for (JFGoodsResp jfGoodsResp : data) {
//			   ImageInfo[] imageInfo = jfGoodsResp.getImageInfo();
//			   System.out.println(jfGoodsResp.getMaterialUrl());
//			   for (ImageInfo imageInfo2 : imageInfo) {
//				   UrlInfo[] imageList = imageInfo2.getImageList();
//				   for (UrlInfo urlInfo : imageList) {
//					   String url = urlInfo.getUrl();
//					   System.out.println("返回数据："+url);
//				   }
//				   System.out.println("....................");
//			   }
//			   System.out.println("----------------------");  
//		}
//		   
//		   
//		   
//	   } catch (JdException e) {
//		   // TODO Auto-generated catch block
//		   e.printStackTrace();
//	}
//   }
    //淘宝
    public static void main(String[] args) {
        try {
            JdClient client = new DefaultJdClient(SERVER_URL_JD, ACCESS_TOKEN_JD, APPKEY_JD, APPSECRET_JD);
            UnionOpenOrderQueryRequest unionOpenOrderQueryRequest = new UnionOpenOrderQueryRequest();
            OrderReq orderReq = new OrderReq();
            orderReq.setPageNo(1);
            orderReq.setPageSize(3);
            orderReq.setType(1);
            orderReq.setTime("2019041716");
            unionOpenOrderQueryRequest.setOrderReq(orderReq);
            UnionOpenOrderQueryResponse response = client.execute(unionOpenOrderQueryRequest);
            OrderResp[] data = response.getData();
            for (OrderResp orderResp : data) {
                System.out.println(orderResp.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

